package edu.dqsy.mzq_translate;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import edu.dqsy.mzq_translate.helper.ContactOpenHelper;

public class Danciliebiao extends AppCompatActivity {
    private ArrayList<words> wordss;
    private ListView lv;
    public String danciname;
    final ContactOpenHelper helper = new ContactOpenHelper(Danciliebiao.this,"userinfo.db",null,1);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_danciliebiao);
        this.showdanci();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        this.showdanci();
    }

    public void addword(View v){
        helper.getWritableDatabase();
        SQLiteDatabase db = helper.getWritableDatabase();
        AlertDialog.Builder builder  = new AlertDialog.Builder(Danciliebiao.this);
        TextView en = findViewById(R.id.word);
        TextView zh = findViewById(R.id.wordzn);
        String eng = en.getText().toString();
        String cn = zh.getText().toString();
        Cursor c = db.rawQuery("SELECT * FROM "+danciname+" where en=?",new String[]{eng});
        if (c.getCount() != 0){
            builder.setTitle("添加失败" ) ;
            builder.setPositiveButton("是" ,  null );
            builder.setMessage(eng+"已存在此单词表中" ) ;
            builder.show();
        }else {
            db.execSQL("Insert into "+danciname+" (zh,en) values(?,?)",new String[]{cn,eng});
            this.showdanci();
        }
    }

    public void showdanci() {
        Intent intentFromSplash = getIntent();
        danciname = intentFromSplash.getStringExtra("danciname");
        System.out.println(danciname);
        helper.getWritableDatabase();
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor c = db.rawQuery("SELECT * FROM "+danciname,null);
        wordss = new ArrayList<>();
        if(c!=null){
            while(c.moveToNext()){
                String zh = c.getString(1);
                String en = c.getString(2);
                System.out.println(zh+":"+en);
                words st = new words(zh,en);
                System.out.println(st.getZh()+"st属性"+st.getEn());
                if (st!=null){
                    wordss.add(st);
                }

            }
        }

        lv = findViewById(R.id.words);

        lv.setAdapter(new BaseAdapter() {
            /*
             * 为ListView设置一个适配器
             * getCount()返回数据个数
             * getView()为每一行设置一个条目
             * */
            @Override
            public int getCount() {
                return wordss.size();
            }

            @Override
            public Object getItem(int position) {
                // return studentlist.get(position);
                return null;
            }

            @Override
            public long getItemId(int position) {

                // return position;
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {


                View view ;
                /**对ListView的优化，convertView为空时，创建一个新视图；
                 * convertView不为空时，代表它是滚出,
                 * 放入Recycler中的视图,若需要用到其他layout，
                 * 则用inflate(),同一视图，用fiindViewBy()
                 * **/
                if(convertView == null )
                {
                    LayoutInflater inflater = Danciliebiao.this.getLayoutInflater();
                    view = inflater.inflate(R.layout.danci,null);
                    //view = View.inflate(getBaseContext(),R.layout.item,null);
                }
                else
                {
                    view = convertView;
                }
                //从studentlist中取出一行数据，position相当于数组下标,可以实现逐行取数据
                words st = wordss.get(position);
                TextView en = (TextView)view.findViewById(R.id.eng);
                TextView zh  = (TextView)view.findViewById(R.id.zh);


                en.setText(st.getEn());
                zh.setText(st.getZh());

                return view;
            }
        });
    }
    public void backtodanci(View v){
        Intent i = new Intent(Danciliebiao.this, Danciben.class);
        startActivity(i);
    }
}

class words{
    String en;
    String zh;

    public words(String en, String zh) {
        this.en = en;
        this.zh = zh;
    }

    public String getEn() {
        return en;
    }

    public void setEn(String en) {
        this.en = en;
    }

    public String getZh() {
        return zh;
    }

    public void setZh(String zh) {
        this.zh = zh;
    }
}